package com.health.healthapplet.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.health.healthapplet.entity.Response;
import com.health.healthapplet.entity.User;
import com.health.healthapplet.service.UserService;
import com.health.healthapplet.utils.EasyPoiUtil;
import com.health.healthapplet.utils.MD5Utils;
import com.health.healthapplet.utils.TokenConstant;
import com.health.healthapplet.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class UserController {
    @Autowired
    private UserService service;
    /**
     * 添加用户
     */
    @RequestMapping(value = "/health-service/addUser",method = RequestMethod.POST)
    public Response addUser(@RequestParam Map<String, String> person){
        String username = person.get("username");
        String password = MD5Utils.md(person.get("password"));
        //判断用户名、密码是否为空
        if(username != null && password != null){
            List<User> users = service.queryByUsername(username);
            if(users != null && users.size()>0){
                return new Response("注册失败，用户名重复",200,true);
            }else{
                int count = service.addUserInfo(username,password);
                if(count>0){
                    //成功
                    return new Response("注册成功",200,true);
                }else{
                    return new Response("注册失败",400,false);
                }
            }
        }else{
            return new Response("注册失败，用户名或密码为空",400,false);
        }
    }
    /**
     * 登录
     */
    @RequestMapping(value = "/health-service/login",method = RequestMethod.POST)
    public Response login(@RequestBody User user){
        Response response = new Response();
        String username = user.getUsername();
        String password = MD5Utils.md(user.getPassword());
        //判断用户名、密码是否为空
        if(username != "" && password != "" && username != null && password != null){
            List<User> users = service.queryByUsername(username);
            if(users != null && users.size()>0){
                User user1 = users.get(0);
                if(password.equals(user1.getPassword())||password ==user1.getPassword()){
                    //4. 密码正确，登陆成功
                    String token = TokenUtils.token(username,password);
                    TokenConstant.updateTokenMap(token);
                    response.setResponse(true,"登录成功",200,token);
                    return response;
                }else {
                    return new Response("登陆失败，密码错误",400,false);
                }
            }else{
                return new Response("你还未注册账号，请注册后再登录",400,false);
            }
        }else{
            return new Response("登录失败，用户名或密码为空",400,false);
        }
    }
    /**
     * 查询列表
     */
    @RequestMapping(value = "/health-service/queryUserPageList",method = RequestMethod.POST)
    public Response queryUserPageList(@RequestParam("pageNum") Integer pageNum,
                                      @RequestParam("pageSize") Integer pageSize, Model model){
        Response response = new Response();
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = service.queryList();
        model.addAttribute("total",new PageInfo<>(userList).getTotal());
        model.addAttribute("data",userList);
        response.setResponse(true,"查询成功",200,model);
        return response;
    }
    /**
     * 导出excel
     */
    @RequestMapping(value = "/health-service/exportUser",method = RequestMethod.POST)
    public void exportUser(HttpServletResponse response){
        List<User> userList = service.queryList();
        EasyPoiUtil.exportExcel(userList,"用户列表-导出","用户列表",User.class,"用户列表",response);
    }
    /**
     * 导入excel
     */
    @RequestMapping(value = "/health-service/importUserExcel", method = RequestMethod.POST)
    public Response importExcel(@RequestParam("file") MultipartFile file, String customerType) {
        try {
            List<User> personList = EasyPoiUtil.importExcel(file, 1, 1, User.class);
            System.out.println("导入数据一共【" + personList.size() + "】行");
            Response response = new Response();
            response.setResponse(true,"操作成功",200,personList);
            for (int i = 0; i < personList.size(); i++) {
                User user = personList.get(i);
                service.addUserInfo(user.getUsername(),user.getPassword());
            }
            return response;

        } catch (Exception e) {
            // TODO: handle exception
            return new Response("导入失败",400,false);
        }
    }
}
